#ifndef PLOTTER_H
#define PLOTTER_H

#include <QString>
#include <QVector>
#include <iostream>
#include "gnuplot-cpp/gnuplot_i.hpp"


class Plotter
{
public:
    Plotter();

    enum PlotTypes
    {
        LINES,
        IMPULSES
    };

    static int counter;

    static Gnuplot * gplot;

    static Gnuplot * getPlotter()
    {
        if(!gplot)
        {
            gplot = new Gnuplot();
        }

        gplot->reset_all();
        gplot->set_terminal_std("png");

        return gplot;
    }

    template <class T>
    static void plotY(QVector<T> * data, PlotTypes type, QString label, QString out)
    {
        try
        {
            Gnuplot * gp = getPlotter();

            setStyle(gp, type);
            gp->cmd("set offsets graph 0.05, 1.05, 0.05, 0.05");
            gp->showonscreen();

            gp->savetops(out.toUtf8().data(), "png");
            gp->plot_x(data->toStdVector(), label.toUtf8().data());
        }
        catch(GnuplotException e)
        {
            std::cout << e.what() << std::endl;
        }
    }

    template <class T, class R>
    static void plotXY(QVector<T> * dataX, QVector<R> * dataY, PlotTypes type, QString label, QString out)
    {
        try
        {
            Gnuplot * gp = getPlotter();

            setStyle(gp, type);
            gp->cmd("set offsets graph 0.05, 0.05");

            gp->savetops(out.toUtf8().data(), "png");
            gp->plot_xy(dataX->toStdVector(), dataY->toStdVector(), label.toUtf8().data());
        }
        catch(GnuplotException e)
        {
            std::cout << e.what() << std::endl;
        }
    }

    static void setStyle(Gnuplot * gp, PlotTypes type)
    {
        switch(type)
        {
            case LINES:
                gp->set_style("lines");
                break;
            case IMPULSES:
            default:
                //gp->set_smooth("bezier");
                gp->set_style("impulses");
                break;
        }
    }
};

int Plotter::counter = 0;
Gnuplot * Plotter::gplot = NULL;

#endif // PLOTTER_H
